<template>
  <div ref="wrapper">
    <slot></slot>
  </div>
</template>

<script>
  import BScroll from 'better-scroll'

  export default {
    name: "scroll",
    props: {
      probeType: {
        type: Number,
        default: 1
      },
      click: {
        type: Boolean,
        default: true
      },
      data: {
        type: Array,
        default: null
      },
      scrollListener: {
        type: Boolean,
        default: false
      },
    },
    mounted() {
      setTimeout(() => {
        this._initScroll()
      }, 20)
    },
    methods: {
      _initScroll() {
        if(!this.$refs.wrapper){
          return
        }
        this.scroll = new BScroll(this.$refs.wrapper, {
          probeType: this.probeType,
          click: this.click,
        })
        if(this.scrollListener){
          let me = this;
          //添加滑动监听,但是传入的probeType要设置为3 否则拿不到快速的滑动的事件
          this.scroll.on('scroll',(pos)=>{
            me.$emit('scroll',pos)
          })
        }
      },
      enable(){
        this.scroll && this.scroll.enable()
      },
      disable(){
        this.scroll && this.scroll.disable()
      } ,
      refresh(){
        this.scroll && this.scroll.refresh()
      },
      scrollElement(){
        this.scroll && this.scroll.scrollToElement.apply(this.scroll,arguments)
      },
      scrollto(){
        this.scroll && this.scroll.scrollTo.apply(this.scroll,arguments)
      }
    },
    watch:{
      data(){
        setTimeout(() => {
          console.log(this.scroll)
          this.refresh()

        }, 20)
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">

</style>
